/***
This figure presents a time series plot of the change in seasonally-adjusted 
consumer spending relative to the base period of January 4-31 for New Mexico and 
Colorado. 
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/State Re-Openings"

*-------------------------------------------------------------------------------
* Prepare reopenings dates  
*-------------------------------------------------------------------------------

* Import data
project, uses("${root}/data/web_d008872/data/Policy Milestones - State.csv")
import delimited "${root}/data/web_d008872/data/Policy Milestones - State.csv", clear

* Transform relevant date variables
foreach var in nonessential_biz_closed regional_biz_opened {
	gen day = substr(`var', 9, 2)
	gen month = substr(`var', 6, 2)
	gen year = substr(`var', 1, 4)
	foreach x in year month day {
		destring `x', replace 
	}
	gen date = mdy(month,day,year) 
	format date %d
	drop `var'
	rename date `var'
	drop day month year 
}

* Get locals for closing and opening dates 
foreach statefips in 8 35 {
	sum nonessential_biz_closed if statefips == `statefips'
	assert `r(N)' == 1 
	local closing_`statefips' = `r(mean)'
	sum regional_biz_opened if statefips == `statefips'
	assert `r(N)' == 1 
	local opening_`statefips' = `r(mean)'
}

*-------------------------------------------------------------------------------
* Prepare Affinity data 
*-------------------------------------------------------------------------------

* Import Affinity data 
project, uses("${root}/data/web/data/Affinity - State - Daily.csv")
import delimited "${root}/data/web/data/Affinity - State - Daily.csv", clear 

* Keep values for one day per week 
assert dow(mdy(2, 1, 2020)) == 6 
gen date = mdy(month, day, year) 
format date %td 
keep if dow(date) == 6 


* Keep values for NM and CO 
keep if inlist(statefips, 8, 35)

* Rescale 
replace spend_all = spend_all * 100 

* Keep dates between 1 February and 6 June 
keep if inrange(date, mdy(2, 1, 2020), mdy(6, 6, 2020))

*-------------------------------------------------------------------------------
* Plot  
*-------------------------------------------------------------------------------

sum date 
local min = `r(min)'
local max = `r(max)'

tw ///
	(connected spend_all date if statefips == 35, color(oi3)) ///
	(connected spend_all date if statefips == 8, color(oi2)) /// 
	, ///
	legend(order(1 "New Mexico" 2 "Colorado") pos(7) cols(1) ring(0)) /// 
	xtitle("") /// 
	ytitle("Change in Consumer Spending (%)" "Relative to January 2020") ///
	ylab(-40 "-40%" -20 "-20%" 0 "0%" 20 "+20%", nogrid) /// 
	xlab(`=mdy(2, 1, 2020)' `""Feb 1" "2020""' ///
		 `=mdy(2, 15, 2020)' "Feb 15" ///
		 `=mdy(2, 29, 2020)' "Feb 29" ///
		 `=mdy(3, 14, 2020)' "Mar 14" ///
		 `=mdy(3, 28, 2020)' "Mar 28" ///
		 `=mdy(4, 11, 2020)' "Apr 11" ///
		 `=mdy(4, 25, 2020)' "Apr 25" ///
		 `=mdy(5, 9, 2020)' "May 9" ///
		 `=mdy(5, 23, 2020)' "May 23" ///
		 `=mdy(6, 6, 2020)' "Jun 6" ///
		 , labsize(small)) ///
	xline(`closing_8', lpattern(dash) lcolor(oi2)) ///
	xline(`opening_8', lpattern(dash) lcolor(oi2)) ///
	xline(`closing_35', lpattern(dash) lcolor(oi3)) ///
	xline(`opening_35', lpattern(dash) lcolor(oi3)) ///
	text(20 22008 "Colorado" "Closing", justification(left) color(oi2)  size(medsmall)) /// 
	text(20 21987 "New Mexico" "Closing", justification(right) color(oi3) size(medsmall)) /// 
	text(10 22021.5 "Colorado Begins" "Re-Opening", justification(right) color(oi2) size(medsmall)) /// 
	text(20 22068 "New Mexico Begins" "Re-Opening", justification(left) color(oi3) size(medsmall)) ///
	graphr(margin(r+6)) ///
	xsize(10) ///
	${title_`version'} 
oi_graph_export "${root}/results/State Re-Openings/Colorado vs New Mexico Re-Opening Event Study", type(${fig_type})
